package com.example.administrator.shzs.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;
/**
 * 修改登陆密码
 */
public class ModifyLoginPasswordActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.et_old_user_password_modify_login_password_activity)
    EditText etOldUserPassword;
    @Bind(R.id.et_new_user_password_modify_login_password_activity)
    EditText etNewUserPassword;
    @Bind(R.id.btn_mondify_login_password)
    Button btnMondifyLoginPassword;

    private String userId=null;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_login_password);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);
        context=this;

        btnMondifyLoginPassword.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        userId= MyApplication.app.getUserId();

        String oldPassword=etOldUserPassword.getText().toString().trim();
        String newPassword=etNewUserPassword.getText().toString().trim();

        switch (v.getId()){

            case R.id.btn_mondify_login_password:

                if(!oldPassword.equals("") && !newPassword.equals("")){
                    //修改密码
                    mondifyLoginPassword(userId,oldPassword,newPassword);
                }

            break;

        }
    }

    /**
     * 修改登陆密码
     * @param userId
     * @param oldPassword
     * @param newPassword
     */
    private void mondifyLoginPassword(String userId, String oldPassword, String newPassword) {

        String url= Contents.USER_MODIFICATION_LOGIN_PASSWORD+userId+"&oldpassword="+oldPassword+"&newpassword="+newPassword;

        RequestParams params=new RequestParams(url);

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {

                    JSONObject object=new JSONObject(result);

                    int state=object.getInt("state");
                    if(state==1){
                        ToastUtils.showMessageShort(context,"修改成功");
                        finish();
                    }else {
                        ToastUtils.showMessageShort(context,"修改失败,请重新输入");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }
}
